package net.eliasbalasis.eclipse.rcp.jnlp.generator.jet.compiled;

import org.eclipse.jet.JET2Context;
import org.eclipse.jet.JET2Template;
import org.eclipse.jet.JET2Writer;
import org.eclipse.jet.taglib.RuntimeTagElement;
import org.eclipse.jet.taglib.TagInfo;
import java.util.*;
import java.io.*;

public class _jet_main implements JET2Template {
    private static final String _jetns_c = "org.eclipse.jet.controlTags"; //$NON-NLS-1$
    private static final String _jetns_ws = "org.eclipse.jet.workspaceTags"; //$NON-NLS-1$

    public _jet_main() {
        super();
    }

    private static final String NL = System.getProperty("line.separator"); //$NON-NLS-1$
    
    private static final TagInfo _td_ws_file_3_1 = new TagInfo("ws:file", //$NON-NLS-1$
            3, 1,
            new String[] {
                "template", //$NON-NLS-1$
                "path", //$NON-NLS-1$
            },
            new String[] {
                "templates/main.jnlp.jet", //$NON-NLS-1$
                "{$org.eclipse.jet.resource.project.name}/export/main.jnlp", //$NON-NLS-1$
            } );
    private static final TagInfo _td_c_setVariable_8_1 = new TagInfo("c:setVariable", //$NON-NLS-1$
            8, 1,
            new String[] {
                "select", //$NON-NLS-1$
                "var", //$NON-NLS-1$
            },
            new String[] {
                "/root/@exportFolder", //$NON-NLS-1$
                "exportFolder", //$NON-NLS-1$
            } );
    private static final TagInfo _td_c_iterate_19_1 = new TagInfo("c:iterate", //$NON-NLS-1$
            19, 1,
            new String[] {
                "select", //$NON-NLS-1$
                "var", //$NON-NLS-1$
            },
            new String[] {
                "/root/feature/@id", //$NON-NLS-1$
                "currentFeatureId", //$NON-NLS-1$
            } );
    private static final TagInfo _td_c_iterate_20_2 = new TagInfo("c:iterate", //$NON-NLS-1$
            20, 2,
            new String[] {
                "select", //$NON-NLS-1$
                "var", //$NON-NLS-1$
            },
            new String[] {
                "/root/feature[@id=$currentFeatureId]/@regex", //$NON-NLS-1$
                "currentFeatureRegex", //$NON-NLS-1$
            } );
    private static final TagInfo _td_ws_file_30_4 = new TagInfo("ws:file", //$NON-NLS-1$
            30, 4,
            new String[] {
                "template", //$NON-NLS-1$
                "path", //$NON-NLS-1$
            },
            new String[] {
                "templates/extension.jnlp.jet", //$NON-NLS-1$
                "{$org.eclipse.jet.resource.project.name}/{$exportFolder}/features/{$currentExtension}", //$NON-NLS-1$
            } );

    public void generate(final JET2Context context, final JET2Writer __out) {
        JET2Writer out = __out;
        out.write(NL);         
        RuntimeTagElement _jettag_ws_file_3_1 = context.getTagFactory().createRuntimeTag(_jetns_ws, "file", "ws:file", _td_ws_file_3_1); //$NON-NLS-1$ //$NON-NLS-2$
        _jettag_ws_file_3_1.setRuntimeParent(null);
        _jettag_ws_file_3_1.setTagInfo(_td_ws_file_3_1);
        _jettag_ws_file_3_1.doStart(context, out);
        _jettag_ws_file_3_1.doEnd();
        out.write(NL);         
        out.write(NL);         
        RuntimeTagElement _jettag_c_setVariable_8_1 = context.getTagFactory().createRuntimeTag(_jetns_c, "setVariable", "c:setVariable", _td_c_setVariable_8_1); //$NON-NLS-1$ //$NON-NLS-2$
        _jettag_c_setVariable_8_1.setRuntimeParent(null);
        _jettag_c_setVariable_8_1.setTagInfo(_td_c_setVariable_8_1);
        _jettag_c_setVariable_8_1.doStart(context, out);
        _jettag_c_setVariable_8_1.doEnd();

Set exportFolderNodeSet = (Set)context.getVariables().get("exportFolder");
String exportFolderString = exportFolderNodeSet.toArray()[0].toString().split("=")[1];
String exportedFeaturesFolderString =
	context.getVariables().get("org.eclipse.jet.resource.parent.rawLocation")+
	"/"+
	exportFolderString+
	"/"+
	"features";

        RuntimeTagElement _jettag_c_iterate_19_1 = context.getTagFactory().createRuntimeTag(_jetns_c, "iterate", "c:iterate", _td_c_iterate_19_1); //$NON-NLS-1$ //$NON-NLS-2$
        _jettag_c_iterate_19_1.setRuntimeParent(null);
        _jettag_c_iterate_19_1.setTagInfo(_td_c_iterate_19_1);
        _jettag_c_iterate_19_1.doStart(context, out);
        while (_jettag_c_iterate_19_1.okToProcessBody()) {
            RuntimeTagElement _jettag_c_iterate_20_2 = context.getTagFactory().createRuntimeTag(_jetns_c, "iterate", "c:iterate", _td_c_iterate_20_2); //$NON-NLS-1$ //$NON-NLS-2$
            _jettag_c_iterate_20_2.setRuntimeParent(_jettag_c_iterate_19_1);
            _jettag_c_iterate_20_2.setTagInfo(_td_c_iterate_20_2);
            _jettag_c_iterate_20_2.doStart(context, out);
            while (_jettag_c_iterate_20_2.okToProcessBody()) {
        
        		String currentFeatureRegexString = context.getVariables().get("currentFeatureRegex").toString().split("=")[1];
        		String extensionRef = null;
        		File exportedFeaturesFolder = new File(exportedFeaturesFolderString);
        		for (File file : exportedFeaturesFolder.listFiles()) {
        			if (file.getName().matches(currentFeatureRegexString)) {
        				extensionRef = file.getName().replaceAll("[.]jar$","")+".jnlp";
        				context.setVariable("currentExtension",extensionRef);
        		
                out.write("\t\t\t");  //$NON-NLS-1$        
                RuntimeTagElement _jettag_ws_file_30_4 = context.getTagFactory().createRuntimeTag(_jetns_ws, "file", "ws:file", _td_ws_file_30_4); //$NON-NLS-1$ //$NON-NLS-2$
                _jettag_ws_file_30_4.setRuntimeParent(_jettag_c_iterate_20_2);
                _jettag_ws_file_30_4.setTagInfo(_td_ws_file_30_4);
                _jettag_ws_file_30_4.doStart(context, out);
                _jettag_ws_file_30_4.doEnd();
                out.write(NL);         
        
        			}
        			
        
        		}
        		
                _jettag_c_iterate_20_2.handleBodyContent(out);
            }
            _jettag_c_iterate_20_2.doEnd();
            _jettag_c_iterate_19_1.handleBodyContent(out);
        }
        _jettag_c_iterate_19_1.doEnd();
    }
}
